<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IntersectionObserver-rootMargin</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      .white-dom {
        height: 710px;
      }
      #target {
        background: rgb(237, 28, 36);
        height: 100px;
        outline: 33px solid rgba(0, 0, 0, 0.2);
      }
      #info {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-size: 2rem;
      }
    </style>
  </head>
  <body>
    <div class="white-dom"></div>
    <div id="target"></div>
    <div class="white-dom"></div>
    <span id="info">初始化</span>
    <script>
      const io = new IntersectionObserver(
        function (entries) {
          console.log('IntersectionObserver触发');
          document.getElementById("info").innerHTML = entries[0].isIntersecting
            ? "元素显示"
            : "元素隐藏";
        },
        {
          rootMargin: "0px 0px 33px 0px"
        }
      );
      io.observe(document.getElementById("target"));
    </script>
  </body>
</html>
